-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket issue #1115
base: master
Are you sure you want to change the base?
Socket issue #1115
Conversation
modern/src/SocketController.js
Outdated
try { | ||
const [devicesResponse, positionsResponse] = await Promise.all([fetch('/api/devices'), fetch('/api/positions')]); | ||
if (devicesResponse.ok) { | ||
dispatch(devicesActions.update(await devicesResponse.json())); | ||
} | ||
if (positionsResponse.ok) { | ||
dispatch(sessionActions.updatePositions(await positionsResponse.json())); | ||
} | ||
connectSocket(); | ||
} catch (error) { | ||
// ignore errors | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to duplicate this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the duplication
modern/src/SocketController.js
Outdated
|
||
useEffect(() => { | ||
if (authenticated) { | ||
setTimeout(keepAlive, 3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this is called only once. Is that by design?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if the value for the useEffect change?
useEffect(() => {
if (authenticated) {
setTimeout(keepAlive, 3000);
}
}, [lastUpdate]);
lastUpdate change every time a data in sent via the socket.onmessage, and the code inside the useEffect is re-executed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see... but how do you cancel the old timeout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about doing that?
const [timeOutId, setTimeOutId] = useState();
...
useEffect(() => {
if (authenticated) {
clearTimeout(timeOutId);
setTimeOutId(setTimeout(keepAlive, 3000));
}
}, [lastUpdate]);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it should work.
The only minor nit is use "timeout" as a single word, not "timeOut".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I push the new changes, I'll give it a try on some devices and came back with the result
modern/src/SocketController.js
Outdated
const resetCounterKeepAlive = () => { | ||
setTimeKeepAlive(new Date()); | ||
}; | ||
|
||
const isConnected = () => Math.abs(new Date() - timeKeepAlive) < 10000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can probably be inlined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True
modern/src/SocketController.js
Outdated
ContentProps={{ | ||
sx: { | ||
background: 'red', | ||
}, | ||
}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move out all unrelated changes like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad
modern/src/SocketController.js
Outdated
@@ -26,11 +29,19 @@ const SocketController = () => { | |||
const [events, setEvents] = useState([]); | |||
const [notifications, setNotifications] = useState([]); | |||
|
|||
const [timeKeepAlive, setTimeKeepAlive] = useState(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably rename this to something like lastUpdate
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that
The main of the solution is it correct? I tried it on iOs and Android, didn't notice an issue on android, but on iOS the problem still persist of socket not updating |
No description provided.